package com.aioething.iot.utils;

import com.alibaba.fastjson.JSON;
import org.apache.http.Consts;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HttpClientUtils {
    public static CloseableHttpResponse postJson(String url, Object obj) throws IOException {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(url);
        StringEntity entity = new StringEntity(JSON.toJSONString(obj), "UTF-8");
        httpPost.setEntity(entity);
        httpPost.setHeader("Content-Type", "application/json;charset=utf8");
        return httpClient.execute(httpPost);
    }

    public static CloseableHttpResponse postJson(String url, NameValuePair[] headers, String json) throws IOException {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(url);
        if (headers != null && headers.length > 0) {
            for (NameValuePair pair : headers) {
                httpPost.addHeader(pair.getName(), pair.getValue());
            }
        }
        httpPost.addHeader("Content-Type", "application/json;charset=utf8");
        StringEntity entity = new StringEntity(json, "UTF-8");
        httpPost.setEntity(entity);
        return httpClient.execute(httpPost);
    }

    public static CloseableHttpResponse post(String url, NameValuePair[] headers, Map<String, Object> map) throws IOException {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(url);
        if (headers != null && headers.length > 0) {
            for (NameValuePair pair : headers) {
                httpPost.addHeader(pair.getName(), pair.getValue());
            }
        }
        List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
        for (String key : map.keySet()) {
            params.add(new BasicNameValuePair(key, String.valueOf(map.get(key))));
        }
        if (!params.isEmpty()) {
            httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
        }
        return httpClient.execute(httpPost);
    }

    public static CloseableHttpResponse post(String url, Map<String, Object> map) throws IOException {
        return post(url, null, map);
    }

    public static CloseableHttpResponse post(String url, NameValuePair[] headers, String data) throws IOException {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(url);
        if (headers != null && headers.length > 0) {
            for (NameValuePair pair : headers) {
                httpPost.addHeader(pair.getName(), pair.getValue());
            }
        }
        StringEntity entity = new StringEntity(data, "UTF-8");
        httpPost.setEntity(entity);
        return httpClient.execute(httpPost);
    }

    public static CloseableHttpResponse put(String url, NameValuePair[] headers, String data) throws IOException {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpPut httpPut = new HttpPut(url);
        if (headers != null && headers.length > 0) {
            for (NameValuePair pair : headers) {
                httpPut.addHeader(pair.getName(), pair.getValue());
            }
        }
        StringEntity entity = new StringEntity(data, "UTF-8");
        httpPut.setEntity(entity);
        return httpClient.execute(httpPut);
    }

    public static CloseableHttpResponse get(String url, Map<String, Object> map, NameValuePair[] headers) throws IOException {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        if (null != map && !map.isEmpty()) {
            List<NameValuePair> params = new ArrayList<>();
            for (String key : map.keySet()) {
                params.add(new BasicNameValuePair(key, map.get(key).toString()));
            }
            UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params, Consts.UTF_8);
            url = url.concat("?").concat(EntityUtils.toString(entity));
        }
        HttpGet httpGet = new HttpGet(url);
        if (null != headers && headers.length > 0) {
            for (NameValuePair pair : headers) {
                httpGet.addHeader(pair.getName(), pair.getValue());
            }
        }
        return httpClient.execute(httpGet);
    }

    public static CloseableHttpResponse get(String url, NameValuePair[] headers) throws IOException {
        return get(url, null, headers);
    }

    public static CloseableHttpResponse get(String url) throws IOException {
        return get(url, null, null);
    }

    public static CloseableHttpResponse delete(String url, NameValuePair[] headers) throws IOException {
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpDelete httpDelete = new HttpDelete(url);
        if (null != headers && headers.length > 0) {
            for (NameValuePair pair : headers) {
                httpDelete.addHeader(pair.getName(), pair.getValue());
            }
        }
        return httpClient.execute(httpDelete);
    }

    public static void main(String[] args) throws IOException {
        Map<String, Object> map = new HashMap<>();
        map.put("by", "mac");
        map.put("ident", "862184057346545");
        CloseableHttpResponse response = HttpClientUtils.get("http://www.baidu.com", map, null);
        System.out.println(response);
    }
}
